import WebHeader from '@/www/components/WebHeader'
import WebFooter from '@/www/components/WebFooter'
import { getQuote, uploadFile, postQuoteOrder } from '@/apiv2/quote'

const CODE = 'OutdoorPrinting'

export default {
  components: { WebHeader, WebFooter },
  data() {
    return {
      dto: undefined,
      quoteData: undefined,
      checkValue: {
        flagCordWrap: false,//包绳包塑料边
        flagGoldSilverWrap: false,//包金银边
        flagSilverMiddleWrap: false,//包银边中边
        flagKtSheet: false,//KT板
        flagPlasticHanging: false,//塑料挂轴
        flagLaminationDoubleSide: false,//双面覆膜
        flagLaminationTwill: false,//加斜纹膜
      },

      quoteValue: {
        category: undefined,
        customQuantity: 1,
        style: 1,
        quantity: undefined,
        size: undefined,
        customLength: undefined,
        customWidth: undefined,
        reserveLength: 0,//包边预留长度
        sizeList: [{
          length: undefined,
          width: undefined,
          quantity: undefined
        }],//尺寸-数量 列表
      },
      customSize:true,
      user: undefined,
      quote: undefined,
      quoteList: undefined,
      detail: undefined,
      fileList: [],
      postData: {
        quantity: undefined,
        detail: undefined,
        amount: undefined,
        fileIds: undefined
      },
      note: undefined,
    }
  },

  created(){
    const user = JSON.parse(localStorage.getItem('user'))
    this.user = user

    getQuote(CODE).then(res => {
      let quoteData = res.config
      this.quoteValue.category = quoteData.cateData.list[0].name
      this.quoteData = quoteData
      this.dto = res
    })
  },

  methods: {

    addNullRowToList(key){
      let list = eval(key)
      list.push({})
      eval(key + '=list')
    },

    //单选互斥
    singleCheckExclude(target,list){
      if(this.checkValue[target]){
        list.forEach(item => {
          this.checkValue[item] = false
        })
      }
    },

    getQuote(){
      let detail = ''
      let {customSize,quoteData,quoteValue,checkValue,postData} = this
      detail += quoteValue.category +'，'

      let totalArea = 0.0
      let totalRound = 0.0
      let totalQuantity = 0

      for(let i = 0; i < quoteValue.sizeList.length; i++){
        let item = quoteValue.sizeList[i]
        if(!item.length || !item.width || !item.quantity){
          this.$message.error('请输入完整尺寸与数量')
          return
        }
        totalArea += parseFloat(item.length) * parseFloat(item.width) / 10000 * parseFloat(item.quantity)
        totalRound += (parseFloat(item.length) + parseFloat(item.width)) * 2 * parseFloat(item.quantity) / 100
        totalQuantity += parseInt(item.quantity)
        detail += item.length + '*' + item.width + 'cm ' + item.quantity + quoteData.param.unit + ','
      }


      let price = undefined
      for(let i = 0; i < quoteData.cateData.list.length; i++){
        let item = quoteData.cateData.list[i]
        if(quoteValue.category == item.name){
          price = item.price
          break
        }
      }

      let discount = 1.0
      for(let i = 0; i < quoteData.process.cateDiscountList.length; i++){
        let item = quoteData.process.cateDiscountList[i]
        if(totalArea >= parseFloat(item.start) && totalArea <= parseFloat(item.end)){
          discount = parseFloat(item.discount)
          break
        }
      }

      let materialAmount = (parseFloat(price) * totalArea * discount).toFixed(2)

      let processAmount = 0.0

      if(checkValue.flagCordWrap){
        let reserveLength = 0
        if(!quoteValue.reserveLength || parseFloat(quoteValue.reserveLength) < 0){
          reserveLength = 0
        }else{
          reserveLength = parseFloat(quoteValue.reserveLength)
        }
        processAmount += parseFloat(quoteData.process.cordWrapPrice) * (totalRound + reserveLength * totalQuantity)
      }

      if(checkValue.flagGoldSilverWrap){
        processAmount += parseFloat(quoteData.process.goldSilverWrapPrice) * totalRound
      }

      if(checkValue.flagSilverMiddleWrap){
        processAmount += parseFloat(quoteData.process.silverMiddleWrapPrice) * totalRound
      }

      if(checkValue.flagSilverMiddleWrap){
        processAmount += parseFloat(quoteData.process.silverMiddleWrapPrice) * totalRound
      }

      if(checkValue.flagKtSheet){
        processAmount += parseFloat(quoteData.process.ktSheetPrice) * totalArea
      }

      if(checkValue.flagPlasticHanging){
        processAmount += parseFloat(quoteData.process.plasticHangingPrice) * totalArea
      }

      if(checkValue.flagLaminationDoubleSide){
        processAmount += parseFloat(quoteData.process.laminationDoubleSidePrice) * totalArea
      }

      if(checkValue.flagLaminationTwill){
        processAmount += parseFloat(quoteData.process.laminationTwillPrice) * totalArea
      }

      let quoteList = [{
        materialAmount: materialAmount,
        quantity: totalQuantity,
        processAmount: processAmount.toFixed(2),
        totalAmount: materialAmount
      }]

      postData.amount = materialAmount
      postData.detail = detail
      this.postData = postData
      this.quoteList = quoteList
    },


    selectQuantity(){
      if(this.quoteValue.quantity){
        this.quoteValue.customQuantity = undefined
      }
    },
    customQuantity(){
      if(this.quoteValue.customQuantity){
        this.quoteValue.quantity = undefined
      }else{
        this.quoteValue.quantity = this.quoteData.stepData.list[0]
      }
    },

    syncCheck(name,target){
      if(this.checkValue[name]){
        this.checkValue[target] = true
      }
    },

    offCheck(name,target){
      if(!this.checkValue[name]){
        this.checkValue[target] = false
      }
    },

    submitOrder(){
      const {fileList,postData} = this

      if(fileList.length < 1){
        this.$message.error('请上传设计稿')
        return
      }

      let fileIds = [];
      fileList.forEach(item => {
        fileIds.push(item.id)
      })
      postData.fileIds = fileIds

      const loading = this.$loading({
        lock: true,
        text: '正在创建订单',
        spinner: 'el-icon-loading',
        background: 'rgba(0,0,0,0.7)'
      });

      postQuoteOrder(postData).then(res => {
        loading.close()
        this.$message.success('成功创建订单')
        this.$router.push({
          path: '/submitOrder',
          query: { id : res.id}
        })
      }).catch(err => {
        loading.close()
        this.$message.error('创建订单失败')
      })
    },

    handleUpload: async function(data){
      const formData = new FormData()
      formData.append('file', data.file)
      const res = await uploadFile(formData)
      let fileList = this.fileList
      fileList.push(res.file)
      this.fileList = fileList
      console.log(fileList)
    },
  }
}
